package core;


import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import view.GUI.LabelReader;


/**
 *
 * @author Wijnand
 */
public class StdNode implements Node, Serializable {
	protected Map<String, Port> ports = new HashMap<String, Port>();
    private String id;
	// = visible to the outside !

    public StdNode(){
        this.id = UUID.randomUUID().toString();
    }
	
	public int portCount() 
	{
		return ports.size();
	}
	
	public boolean hasPort(String s) 
	{
		return ports.containsKey(s);
	}
	
	public Port getPort(String s) {
		return ports.get(s);
	}


	
	// adds/replaces
	public void putPort(String s, Port p)
	{
		if (s==null){
			throw new RuntimeException(LabelReader.getInstance().getString("PortnameCannotBeNull"));
                }
                if (p==null){
			throw new RuntimeException(LabelReader.getInstance().getString("PortCannotBeNull"));
                }
                ports.put(s, p);
	}
	
	
	// add all Port-attributes to port-map
	protected void autoPutPorts() 
	{
		//System.out.println(this.getClass().getName() + ":");
		try 
		{
			for (Field f: this.getClass().getDeclaredFields())
				if (Port.class.isAssignableFrom(f.getType()))
				{
					f.setAccessible(true);
					this.ports.put(f.getName(), (Port)f.get(this));
				}
		}	
		catch(Exception e)
		{
			throw new RuntimeException(e);
		}
	}

	public Map<String, Port> getPortMap() {
		return Collections.unmodifiableMap(ports);
	}

    public String getId() {
        return id;
    }


}
